package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.doublefish.doublefishrobot.R;

/**
 * 作者:dugaofeng on 2024/12/23 09:51
 */
public class ModeSettingItemSlider extends RelativeLayout {

    private Context mContext;

    private View bgLine;
    private View offsetView;
    private CardView seekTv;

    private static final int DEFAULT_MAX_WIDTH = 10000;
    private int maxWidth = DEFAULT_MAX_WIDTH;
    private boolean isMoving = false; // 是否移动中
    private float lastX;

    private int cur;
    private int min;
    private int max;

    private OnModeSettingItemSliderListner onModeSettingItemSliderListner;
    public ModeSettingItemSlider(@NonNull Context context) {
        this(context, null);
    }

    public ModeSettingItemSlider(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModeSettingItemSlider(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.dialog_mode_setting_slider, this);

        bgLine = findViewById(R.id.view_bg);
        offsetView = findViewById(R.id.view_offsetx);
        seekTv = findViewById(R.id.tv_seek);
    }


    public void setMaxWidth(int maxWidth) {
        this.maxWidth = maxWidth;
        updateOffsetViewWidth();
    }

    public void setValue(int cur, int min, int max) {
        this.cur = cur;
        this.min = min;
        this.max = max;

        updateOffsetViewWidth();
    }

    private void updateOffsetViewWidth() {
        if (maxWidth == DEFAULT_MAX_WIDTH) {
            return;
        }
        int width = 0;
        if (max - min > 0) {
            int progress = (cur - min) * 100 / (max - min);
            width = maxWidth * progress / 100;
        }
        ViewGroup.LayoutParams params = offsetView.getLayoutParams();
        params.width = width;
        offsetView.setLayoutParams(params);
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (max - min <= 0) {
            return true;
        }
        int action = event.getAction();

        if (action == MotionEvent.ACTION_DOWN) {
            //点击的位置
            float x = event.getX();
            float y = event.getY();
            //按钮的位置 尺寸
            float seekTvX = seekTv.getX();
            float seekTvY = seekTv.getY();
            int seekTvWidth = seekTv.getMeasuredWidth();  //宽高
            //判断点击的位置是否在按钮的范围
            if (x > seekTvX && x < seekTvX + seekTvWidth && y > seekTvY && y < seekTvY + seekTvWidth) {
                isMoving = true;
                lastX = x;
            }
        } else if (action == MotionEvent.ACTION_MOVE && isMoving) {
            float x = event.getX();
            int offset = (int)(x - lastX);
            if (offset == 0) {
                return true;
            }

            ViewGroup.LayoutParams params = offsetView.getLayoutParams();
            int width = params.width + offset;
            if (width<0) {
                width = 0;
            } else if (width > maxWidth) {
                width = maxWidth;
            }
            params.width = width;
            offsetView.setLayoutParams(params);
            //计算当前值
            int progress = width * 100 / maxWidth;
            cur = (max - min) * progress / 100 + min;

            if (this.onModeSettingItemSliderListner != null) {
                this.onModeSettingItemSliderListner.onValueChanged(cur);
            }
            lastX = x;
        } else {
            isMoving = false;
        }
        return true;
    }

    public void setOnModeSettingItemSliderListner(OnModeSettingItemSliderListner onModeSettingItemSliderListner) {
        this.onModeSettingItemSliderListner = onModeSettingItemSliderListner;
    }

    public interface OnModeSettingItemSliderListner {
        void onValueChanged(int cur);  //0-100
    }
}

